home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
batchut
/
tsbat45.zip
/
BATRICKS.TXT
< prev
next >
Wrap
Text File
|
1994-05-31
|
35KB
|
934 lines
Assorted Batch Tricks Mon 30-May-94
=====================
All rights reserved
Copyright (c) 1993, 1994 by Timo Salmi
..................................................................
Prof. Timo Salmi Co-moderator of comp.archives.msdos.announce
Moderating at garbo.uwasa.fi anonymous FTP archives 128.214.87.1
Faculty of Accounting & Industrial Management; University of Vaasa
Internet: ts@uwasa.fi BBS +(358)-61-3170972; FIN-65101, Finland
..................................................................
┌───────────────────────────────────────────────────────────┐
│ This file belongs to TSBAT*.ZIP. Please do not distribute │
│ this batricks.txt file separately! │
└───────────────────────────────────────────────────────────┘
Introduction
============
This file contains assorted batch tricks. Many, but not all, have
been used in the TSBAT*.ZIP collection of batches. Likewise, there
are some useful further tricks, not documented here, to be found in
the TSBAT batches. Furthermore, many users have sent me useful
suggestions and their own alternative solutions. My best thanks for
the material. You can find much of this feedback stored in the
garbo.uwasa.fi:/pc/pd2/tspost*.zip files.
You are free to quote brief passages from this file provided you
clearly indicate the source with a proper acknowledgment.
Comments and corrections are solicited. But if you wish to have
individual batch programming consultation, please rather post your
question to a UseNet newsgroup like comp.os.msdos.programmer. It is
much more efficient than asking me by email. I'd like to help, but I
am very pressed for time. I prefer to pick the questions I answer
from the Usenet news. Thus I can answer publicly at one go if I
happen to have an answer. Besides, newsgroups have a number of
readers who might know a better or an alternative answer. Don't be
discouraged, though, if you get a reply like this from me. I am
always glad to hear from fellow batch file users.
INDEX
=====
1) Making "@echo off" general
2) Deleting all files
3) Nested loops
4) Checking whether a directory exists
5) Checking that a program is available at the current directory or at path
6) Using subroutines in batches
7) Convert a parameter to uppercase
8) Appending a new directory to the path
9) Comparing two files
10) Writing an empty line
11) Customizing the pause message
12) Complicate renaming with for
13) Checking for wildcards
14) Preventing breaking the batch
15) Prevent a break from bypassing your autoexec.bat
16) Getting the extension
17) The quote character %
18) Eliminating auxiliary batches
19) Utilizing the subst command in paths
20) How to run a batch once a week (testing for the weekday)
21) Testing if a file name includes a path
22) Showing the time without enter
23) Alternatives for testing for the errorlevel value
24) Redirecting a batch file's output
25) Testing for environment space sufficiency
26) A simple trick to "disable" a drive
27) Sending an escape sequence to the printer
28) Creating a random string
29) Finding out the length of a string
30) How to obtain the MsDos version into an environment variable
31) Finding out the number of regular files on a drive
32) Augmenting line numbers to a text file
33) Storing and returning to the original directory (push and pop)
34) Enticing the current date into an environment variable
35) Identifying the individual PC
36) For loop and redirection quirks
37) Traversing a directory tree
38) Echoing the redirection symbol
39) Getting the file basename
1. Making "@echo off" general
=============================
If you want to turn the echo off, and do not wish to show that line
on the screen, you can easily do this by applying
@echo off
There is a catch, however, because this only works since MsDos
version 3.30. So if you want to make it general, put the following
line in your autoexec.bat file if you are using MsDos 3.30 or higher
set _echo=@
Then use the following format in your batches, which will then work
for any MsDos version
%_echo%echo off
2. Deleting all files
=====================
One of the most Frequently Asked Questions (FAQs) about batches is
how to suppress the "Are you sure (Y/N)?" confirmation requirement
for del *.*. Use the following:
echo y| del *.*
If you wish to suppress the message too, use
echo y| del *.* > nul
Whether or not it is sensible to suppress the confirmation can be
debated, but this is the trick anyway.
3. Nested loops
===============
It is possible to have nested loops of a kind in batch programming.
Consider the following two batches, and try it out by calling
test.bat.
echo off
rem TEST.BAT
for %%f in (a b c d e f) do %comspec% /c test2 %%f
echo off
rem TEST2.BAT
for %%g in (1 2 3) do echo %1%%g
Alternatively write everything below on a single line
for %%f in (a b c d e f) do %comspec% /c
for %%g in (1 2 3) do echo %%f%%g
(The wrap has been used in the text is because of the right margin.
Don't wrap your batch.). The disadvantage of this alternative is
that the echo will be on.
4. Checking whether a directory exists
======================================
It is sometimes useful to be able to test whether a particular
directory exists. The following test is true if the %1 directory
does not exist.
if not exist %1\nul if not exist %1nul echo Directory %1 does not exist
5. Checking that a program is available at the current directory or at path
===========================================================================
When you call a program from a batch, and do not give the explicit
path to it, it is advisable to test that the program is available
either at the current directory or the default path.
set _found=
if exist %1 set _found=yes
for %%d in (%path%) do if exist %%d\%1 set _found=yes
for %%d in (%path%) do if exist %%d%1 set _found=yes
if "%_found%"=="yes" goto _continue
echo %1 is not at path or the current directory
goto _out
:_continue
echo %1 found at path or in the current directory
:_out
6. Using subroutines and recursion in batches
=============================================
It is possible to use subroutines within batches. The crucial trick
is setting an environment variable (eg _return) to point to a label
where to return after the subroutine has been performed. For an
example see UNPACK.BAT, and BOOT.BAT, the sections :_common and
:_subru.
Likewise it is possible to use recursion go emulate subroutines in
batches. (Recursion means that a batch calls itself).
As an example see SAFEDEL.BAT and trace the effects of the line
for %%f in (%1) do call safedel %%f recurse
Note that safedel could be replaced by %0 because the zeroeth
parameter of a batch file points to itself.
7. Convert a parameter to uppercase
===================================
This example shows how to ensure that the parameter %1 given to the
batch is in uppercase. This utilizes the fact that MsDos converts
the path to uppercase. The result is stored in upcase_ and then the
original path is restored.
set tmp_=%path%
path=%1
set upcase_=%path%
path=%tmp_%
set tmp_=
The also is another method for getting case-independent results.
This is adapted from Jeff Prosise's column in PC Computing, March
1993, pp. 216-217. If the batch below is called TEST.BAT, it makes
no diffirence whether you enter "TEST yes" or "TEST YES" or "TEST
yEs".
@echo off
if not "%1"=="" set %1=*****
set status_=
if "%yes%"=="*****" set status_=yes
if "%no%"=="*****" set status_=no
if not "%status_%"=="" echo The parameter %%1 was a %status_%
if "%status_%"=="" echo The parameter %%1 was neither a yes nor a no
if not "%1"=="" set %1=
8. Appending a new directory to the path
========================================
This often needed trick is basically very simple. For example
to add direc